package com.lc.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.lc.exception.ControllerException;
import com.lc.service.TestService;
import com.lc.vo.TestVo;

@Controller
@RequestMapping("")
public class TestController {
	
	@Autowired(required=false)
	private TestService testService;
	
	@RequestMapping("test")
	public String test() throws Exception {
		return "test";
	}
	
	/**
	 * 统一异常处理
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exp")
	public String exp(String id) throws Exception {
		testService.thorwServiceException(id);
		if("1".equals(id)) {
			throw new ControllerException("controller exception");
		}
		return "exp";
	}
	
	@RequestMapping(value = "/restful/jsp", method = RequestMethod.GET)
	public String jsp(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
		return "restful";
	}
	
	/**
	 * restful风格list接口
	 * @param request
	 * @param response
	 * @param vo
	 * @throws IOException
	 */
	@RequestMapping(value = "/restful", method = RequestMethod.GET)
	public void list(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
		System.out.println("list被访问，参数：" + vo.toString());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("params", vo);
		map.put("method", RequestMethod.GET);
		response.getWriter().write(JSON.toJSONString(map));
	}
	
	/**
	 * restful风格update接口
	 * @param request
	 * @param response
	 * @param vo
	 * @throws IOException
	 */
	@RequestMapping(value = "/restful", method = RequestMethod.POST)
	public void update(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
		System.out.println("update被访问，参数：" + vo.toString());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("params", vo);
		map.put("method", RequestMethod.POST);
		response.getWriter().write(JSON.toJSONString(map));
	}
	
	/**
	 * restful风格add接口
	 * @param request
	 * @param response
	 * @param vo
	 * @throws IOException
	 */
	@RequestMapping(value = "/restful", method = RequestMethod.PUT)
	public void add(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
		Enumeration<String> params = request.getParameterNames();
		while(params.hasMoreElements()) {
			System.out.println(params.nextElement());
		}
		System.out.println("add被访问，参数：" + vo.toString());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("params", vo);
		map.put("method", RequestMethod.PUT);
		response.getWriter().write(JSON.toJSONString(map));
	}
	
	/**
	 * restful风格add接口
	 * @param request
	 * @param response
	 * @param vo
	 * @throws IOException
	 */
	@RequestMapping(value = "/restful/{id}", method = RequestMethod.DELETE)
	public void del(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id) throws IOException {
		Enumeration<String> params = request.getParameterNames();
		while(params.hasMoreElements()) {
			System.out.println(params.nextElement());
		}
		System.out.println("delete被访问，参数："  + ", id:" + id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("params", id);
		map.put("method", RequestMethod.DELETE);
		response.getWriter().write(JSON.toJSONString(map));
	}
	
}
